<template>
	<view class="wrap">
	</view>
</template>
<script>
	/**
	 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
	 */
	import base64 from '@/common/base64.js';
	import {validatenull} from '@/common/validate.js'
	import md5 from '@/common/md5.min.js'

	export default {
		data() {
			return {
				phoneNo: '',
				username: '',
				password: '',
				loginType: 'currentPhone',
				showPassword: false,
				remember: true,
				isValidCodeLogin: false,
				validCode: '',
				imgValidCodeSrc: null,
				list: [{
					name: '用户名'
				}, {
					name: '手机号'
				}],
				current: 0,
				activeColor: '#007aff',
			};
		},
		onLoad(param) {
			//有存在token情况
			if (!validatenull(this.vuex_accessToken) 
			&& !validatenull(this.vuex_loginSource) 
			&& this.vuex_loginSource==='1') {
				let redirect=this.getRedirectUrl();
				//再次验证token是否失效
				this.$u.api.index({
					loginCheck: true
				}).then(res => {
					if (res.code == '200') {
						this.setPermission(this);
						//如果存在多角色
						if(this.vuex_config.switchMode && (this.vuex_roleId.includes(",")||this.vuex_deptId.includes(","))){
							if(!validatenull(redirect)){
								//保存重定向地址
								uni.setStorageSync('redirect', redirect);
							}
							// //跳转到切换多角色页面
							// uni.reLaunch({
							// 	url: '/pages/sys/user/switchRole'
							// });
							this.goPage();
						}else{
							if(!validatenull(redirect)){
								uni.setStorageSync('redirect', redirect);
							}
							this.goPage();
						}
					}
				});
				
			}else{
				if(!validatenull(this.$route.query.code)){
					this.$u.api.casLogin({
						tenantId: this.vuex_config.tenantId,
						source: this.vuex_config.source,
						code: this.$route.query.code,
						state: this.$route.query.state,
						grant_type: this.vuex_config.source,
					    scope: "all",
					}).then(res => {
						if(res.access_token){
							setTimeout(() => {
								this.setPermission(this);
								let redirect=this.getRedirectUrl();
								if(!validatenull(redirect)){
									uni.setStorageSync('redirect', redirect);
								}
								if(this.vuex_config.switchMode && res.role_id.includes(",")){
									uni.reLaunch({
										url: '/pages/sys/user/switchRole'
									});
								}else{
									this.goPage();
								}
							}, 1000);
						}else if (res.error) {
							uni.showToast({
								title: res.error_description,
								duration: 2000
							});
							window.location.href=this.vuex_config.authUrl+"/cas_ticket";
						}else{
							//404或者未连接服务器返回的html错误
							uni.showToast({
								title: "未连接到服务器！",
								duration: 2000
							});
						}
					});
				}
			}
		},
		methods: {
			showPass() {
				this.showPassword = !this.showPassword;
			},
			qiehuanLogin() {
				this.loginType = 'other'
			},
			onClickItem(index) {
				this.current = index;
			},
			refreshImgValidCode(e) {
				if (this.vuex_token == '') {
					this.$u.api.index().then(res => {
						this.imgValidCodeSrc = this.vuex_config.baseUrl + '/validCode?__sid=' +
							res.sessionid + '&t=' + new Date().getTime();
					});
				} else {
					this.imgValidCodeSrc = this.vuex_config.baseUrl + '/validCode?__sid=' +
						this.vuex_token + '&t=' + new Date().getTime();
				}
				this.validCode = '';
			},
			nextStep() {
				//验证码登录下一步
				uni.showLoading({
					title: '正在获取验证码',
					mask: true
				})
				this.$u.api.sendCode({
						phoneNo: this.phoneNo,
						validCodeType: '2'
					})
					.then(res => {
						if (res.code == '200') {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/sys/login/code?phoneNo=' + this.phoneNo
								});
							}, 1000);
						} else {
							this.$u.toast(res.msg);
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/sys/login/code?phoneNo=' + this.phoneNo
								});
							}, 500);
						}
					});
			},
			submit(loginType) {
				if (this.username.length == 0) {
					this.$u.toast('请输入账号');
					return;
				}
				if (this.password.length == 0) {
					this.$u.toast('请输入密码');
					return;
				}
				this.$u.api.login({
						username: this.username,
						password: md5(this.password),
						grant_type: 'password',
						refresh_token: '',
						tenant_id: this.vuex_config.tenantId,
					})
					.then(res => {
						if(res.access_token){
							uni.showToast({
								title:"登录成功",
								duration:4000
							})
							setTimeout(() => {
								this.setPermission(this);
								//验证是否有前面页，有就跳转
								if(this.vuex_config.switchMode && res.role_id.includes(",")){
									uni.reLaunch({
										url: '/pages/sys/user/switchRole'
									});
								}else{
									this.goPage();
								}
							}, 500);
							if (res.isValidCodeLogin) {
								this.isValidCodeLogin = true;
								this.refreshImgValidCode();
							}
						}else if(res.error){
							uni.showToast({
								title: res.error_description,
								duration: 2000
							});
						}else{
							uni.showToast({
								title: "未连接到服务器！",
								duration: 2000
							});
						}
					});
			},
			wxLogin(res) {
				this.$u.toast('微信登录');
			},
			qqLogin() {
				this.$u.toast('QQ 登录');
			},
			/**
			 * 获取重定向地址
			 */
			getRedirectUrl(){
				let json=this.$route.query;
				if (json.hasOwnProperty("redirect")) {
					let redirect=this.$route.query.redirect;
					let map={};
					let params= Object.keys(json).map(function(key) {
						if(key==='redirect' || key==='code' || key==='state'){
							return ;
						}
						return key + '=' + json[key];
					}).filter(item=>item!=undefined).join('&');
					if(params!=null && params!='' && params!=undefined){
						redirect=redirect+'&'+params;
					}
					return redirect;
				}
			}
		}
	};
</script>
<style lang="scss">
	@import 'index.scss';

	.logo {
		width: 80%;
		font-size: 64rpx;
		color: #5473e8;
		margin: 80rpx auto 80rpx auto;
	}

	.list-call-icon {
		color: #ff0000;
	}

	.currentPhone-box {
		text-align: center;
		padding: 40rpx 80rpx;

		.number-text {
			color: #000000;
			font-size: 60rpx;
		}

		.other-text {
			color: #999999;
			font-size: 26rpx;
			padding: 20rpx 0;
		}

		.u-btn {
			margin: 30rpx auto;
		}

		.u-hairline-border {
			border: 1px solid #fff;
		}
	}

	.register {
		display: inline-block;
		color: #5473e8;
		height: 40rpx;
		line-height: 40rpx;
		font-size: 28rpx;
		float: right;
		margin-top: 6rpx;
	}

	.register-link {
		float: right;
		padding: 0 16rpx;
	}

	.reg-link {
		display: inline-block;
		color: #5473e8;
	}

	.oauth2 {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		margin: 0rpx 100rpx 30rpx;

		image {
			height: 80rpx;
			width: 80rpx;
		}
	}

	.u-tabs {
		padding: 0 70rpx;
	}
</style>
